<%@ page import="db.MaintenanceDAO" %>
<%@ page import="beans.MaintenanceBean" %>
<%@ page import="java.util.List" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.sql.SQLException" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>公司用车管理系统（财务）</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: "Microsoft YaHei", Arial, sans-serif;
            background: #f6f6f6;
        }
        .header {
            background: #183a8a;
            color: #fff;
            font-size: 2rem;
            font-weight: bold;
            padding: 10px 0 10px 10px;
            letter-spacing: 2px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .header-right {
            display: flex;
            align-items: center;
            margin-right: 30px;
            font-size: 1rem;
        }
        .user-icon {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background: #fff;
            color: #183a8a;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 2rem;
            margin-right: 10px;
        }
        .user-info {
            margin-right: 20px;
            color: #fff;
        }
        .bell-icon {
            font-size: 1.3rem;
            margin-right: 20px;
            color: #fff;
        }
        .logout {
            color: #fff;
            text-decoration: none;
            margin-left: 10px;
            font-size: 1rem;
        }
        .container {
            display: flex;
            height: calc(100vh - 56px);
        }
        .sidebar {
            width: 220px;
            background: #183a8a;
            display: flex;
            flex-direction: column;
            padding-top: 10px;
            box-shadow: 2px 0 8px rgba(24,58,138,0.05);
        }
        .menu-item {
            color: #fff;
            padding: 18px 0 18px 32px;
            font-size: 1.15rem;
            cursor: pointer;
            border-left: 5px solid transparent;
            transition: background 0.2s, border 0.2s;
            text-decoration: none;
            display: block;
        }
        .menu-item.active, .menu-item:hover {
            background: #2450a7;
            border-left: 5px solid #fff;
            font-weight: bold;
            box-shadow: 0 4px 8px rgba(24,58,138,0.10);
        }
        .main-content {
            flex: 1;
            background: #f6f6f6;
            border: 2px solid #3ba0e9;
            border-left: none;
            padding: 20px;
        }
        .expenseClaim {
            background: #fff;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }

        .claim-status button {
            padding: 5px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 5px;
        }

        .claim-status button.completed {
            background: #d4edda;
            color: #155724;
        }

        .claim-list {
            max-height: 400px;
            overflow-y: auto;
            margin-top: 20px;
        }
        .claim-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px 0;
            border-bottom: 1px solid #ddd;
        }
        .claim-item:last-child {
            border-bottom: none;
        }
        .claim-item .status {
            background: #f0f0f0;
            padding: 5px 10px;
            border-radius: 4px;
            color: #333;
        }
        .claim-item .status.ongoing {
            background: #d1e7dd;
            color: #0f5132;
        }
        .claim-item .status.review {
            background: #fff3cd;
            color: #664d03;
        }
        .claim-item .status.completed {
            background: #d4edda;
            color: #0f5132;
        }

        /* 新增搜索框样式 */
        .search-section {
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
            margin-bottom: 20px;
            padding: 15px;
            background: #f8f9fa;
            border-radius: 5px;
        }
        .search-field {
            display: flex;
            flex-direction: column;
            min-width: 180px;
        }
        .search-field label {
            margin-bottom: 5px;
            font-weight: bold;
            color: #495057;
        }
        .search-field input, .search-field select {
            padding: 8px 12px;
            border: 1px solid #ced4da;
            border-radius: 4px;
            font-size: 14px;
        }
        .search-buttons {
            display: flex;
            align-items: flex-end;
            gap: 10px;
        }
        .search-btn {
            padding: 8px 20px;
            background-color: #183a8a;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }
        .search-btn:hover {
            background-color: #2450a7;
        }
        .reset-btn {
            padding: 8px 20px;
            background-color: #6c757d;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
        }
        .reset-btn:hover {
            background-color: #5a6268;
        }
        .no-records {
            text-align: center;
            padding: 20px;
            color: #6c757d;
            font-size: 1.1rem;
        }

        .error-message {
            color: #dc3545;
            padding: 15px;
            background-color: #f8d7da;
            border-radius: 4px;
            margin-top: 20px;
        }

        .claim-item > div {
            flex: 1;
            min-width: 120px;
            padding: 0 5px;
        }

        .claim-item .status {
            min-width: 80px;
            text-align: center;
        }
        .claim-item .action-btn {
            padding: 5px 10px;
            background-color: #183a8a;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            text-decoration: none;
            display: inline-block;
            margin-left: 10px;
        }

        .claim-item .action-btn:hover {
            background-color: #2450a7;
        }

        /* 调整claim-item的布局 */
        .claim-item > div {
            flex: 1;
            min-width: 100px;
            padding: 0 5px;
        }

        .claim-item .status {
            min-width: 80px;
            text-align: center;
        }

        .claim-item .actions {
            min-width: 100px;
            text-align: center;
        }
    </style>
</head>
<body>
<div class="header">
    <div>欢迎登录公司用车管理系统（财务）</div>
    <div class="header-right">
        <div class="user-icon">👤</div>
        <div class="user-info">
            用户: <%= session.getAttribute("username") %>
            (<%= session.getAttribute("role") %>)
        </div>
        <a href="LogoutServlet" class="logout">退出登录</a>
    </div>
</div>
<div class="container">
    <div class="sidebar">
        <a class="menu-item" href="firstpage.jsp">系统首页</a>
        <a class="menu-item" href="Profile.jsp">个人中心</a>
        <a class="menu-item" href="myTo-Do.jsp">我的待办</a>
        <a class="menu-item" href="Cost.jsp">用车费用报销单</a>
        <a class="menu-item active" href="expenseClaim.jsp">维修费用报销单</a>
    </div>
    <div class="main-content">
        <div class="expenseClaim">
            <!-- 搜索区域 -->
            <form method="get" action="expenseClaim.jsp">
                <div class="search-section">
                    <div class="search-field">
                        <label for="plateNumber">车牌号</label>
                        <input type="text" id="plateNumber" name="plateNumber"
                               value="<%= request.getParameter("plateNumber") != null ? request.getParameter("plateNumber") : "" %>"
                               placeholder="请输入车牌号">
                    </div>

                    <div class="search-field">
                        <label for="status">状态</label>
                        <select id="status" name="status">
                            <option value="">全部状态</option>
                            <option value="待审核" <%= "待审核".equals(request.getParameter("status")) ? "selected" : "" %>>待审核</option>
                            <option value="已通过" <%= "已通过".equals(request.getParameter("status")) ? "selected" : "" %>>已通过</option>
                            <option value="未通过" <%= "未通过".equals(request.getParameter("status")) ? "selected" : "" %>>未通过</option>
                        </select>
                    </div>

                    <div class="search-field">
                        <label for="startDate">开始日期</label>
                        <input type="date" id="startDate" name="startDate"
                               value="<%= request.getParameter("startDate") != null ? request.getParameter("startDate") : "" %>">
                    </div>

                    <div class="search-field">
                        <label for="endDate">结束日期</label>
                        <input type="date" id="endDate" name="endDate"
                               value="<%= request.getParameter("endDate") != null ? request.getParameter("endDate") : "" %>">
                    </div>

                    <div class="search-field">
                        <label for="drivers">司机</label>
                        <input type="text" id="drivers" name="drivers"
                               value="<%= request.getParameter("drivers") != null ? request.getParameter("drivers") : "" %>"
                               placeholder="请输入司机姓名">
                    </div>

                    <div class="search-buttons">
                        <button type="submit" class="search-btn">搜索</button>
                        <a href="expenseClaim.jsp" class="reset-btn">重置</a>
                    </div>
                </div>
            </form>

            <div class="claim-list">
                <%
                    // 获取搜索参数
                    String plateNumber = request.getParameter("plateNumber");
                    String status = request.getParameter("status");
                    String startDate = request.getParameter("startDate");
                    String endDate = request.getParameter("endDate");
                    String drivers = request.getParameter("drivers");

                    MaintenanceDAO dao = new MaintenanceDAO();
                    try {
                        List<MaintenanceBean> records;

                        // 如果有搜索条件，调用搜索方法
                        if (plateNumber != null && !plateNumber.isEmpty() ||
                                status != null && !status.isEmpty() ||
                                startDate != null && !startDate.isEmpty() ||
                                endDate != null && !endDate.isEmpty() ||
                                drivers != null && !drivers.isEmpty()) {

                            records = dao.searchMaintenanceRecords(plateNumber, status, startDate, endDate, drivers);
                        } else {
                            // 没有搜索条件，获取所有记录
                            records = dao.getMaintenanceRecords();
                        }

                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

                        if (records != null && !records.isEmpty()) {
                            for (MaintenanceBean record : records) {
                                String statusClass = "";
                                switch(record.getStatus()) {
                                    case "待审核": statusClass = "review"; break;
                                    case "已通过": statusClass = "completed"; break;
                                    case "未通过": statusClass = "ongoing"; break;
                                }
                                %>
                                <div class="claim-item">
                                    <div>单号: <%= record.getMaintenanceId() %></div>
                                    <div>车牌号: <%= record.getLicensePlate() != null ? record.getLicensePlate() : "N/A" %></div>
                                    <div>司机: <%= record.getDriverName() != null ? record.getDriverName() : "N/A" %></div>
                                    <div>用途: <%= record.getPurpose() != null ? record.getPurpose() : "N/A" %></div>
                                    <div>维修费用: <%= record.getCost() != null ? "¥" + record.getCost() : "N/A" %></div>
                                    <div>日期: <%= record.getMaintenanceTime() != null ? dateFormat.format(record.getMaintenanceTime()) : "N/A" %></div>
                                    <div class="status <%= statusClass %>"><%= record.getStatus() %></div>
                                    <div class="actions">
                                        <a href="viewMaintenance.jsp?id=<%= record.getMaintenanceId() %>" class="action-btn">查看</a>
                                    </div>
                                </div>
                                <%
                            }
                        } else {
                                %>
                                <div class="no-records">没有找到符合条件的记录</div>
                                <%
                        }
                    } catch (SQLException e) {
                    e.printStackTrace();
                %>
                <div class="error-message">查询数据库时出错: <%= e.getMessage() %></div>
                <%
                    }
                %>
            </div>
        </div>
    </div>
</div>
</body>
</html>